package org.mms.controller;

import org.mms.pojo.dto.FeedbackDto;
import org.mms.pojo.entity.Feedback;
import org.mms.pojo.request.FeedbackCreateRequest;
import org.mms.pojo.request.FeedbackReplyRequest;
import org.mms.pojo.util.ResponseMessage;
import org.mms.service.FeedbackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/feedbacks")
public class FeedbackController {

    @Autowired
    private FeedbackService feedbackService;

    @GetMapping
    public ResponseMessage<List<FeedbackDto>> listFeedbacks() {
        return ResponseMessage.success(feedbackService.list()
                .stream()
                .map(Feedback::convert)
                .toList());
    }

    @GetMapping("/{id}")
    public ResponseMessage<FeedbackDto> getFeedbackById(@PathVariable Long id) {
        return ResponseMessage.success(feedbackService.getById(id)
                .convert());
    }

    @PostMapping
    public ResponseMessage<FeedbackDto> createFeedback(@RequestBody FeedbackCreateRequest request) {
        return ResponseMessage.success(feedbackService.create(request)
                .convert());
    }

    @PutMapping("/reply")
    public ResponseMessage<FeedbackDto> replyToFeedback(@RequestBody FeedbackReplyRequest request) {
        return ResponseMessage.success(feedbackService.replyTo(request)
                .convert());
    }

    @DeleteMapping("/{id}")
    public ResponseMessage<?> deleteFeedback(@PathVariable Long id) {
        feedbackService.delete(id);
        return ResponseMessage.success(null);
    }
}